package net.esj.basic.core.business.provider;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;

import net.esj.basic.core.engine.factory.bean.BeanFactory;
import net.esj.basic.pojo.MObject;
import net.esj.basic.pojo.business.Holder;

@Singleton
@Named("melon.MObjectOwnerProvidersFactory")
public class MObjectOwnerProvidersFactory {

	private static Map<String,MObjectOwnerProvider> providers = new HashMap<String,MObjectOwnerProvider>();

	private BeanFactory beanFactory;
	
	 public static Holder getOwner(MObject arg){
		 MObjectOwnerProvider provider = providers.get(arg.getMType());
		if(provider!=null){
			return provider.getOwner(arg);
		}
		return Holder.EMPTY;
	}
	
	 @Inject
	public void setBeanFactory(BeanFactory beanFactory) {
		this.beanFactory = beanFactory;
		Map<String,MObjectOwnerProvider> pros = beanFactory.getBeansByType(MObjectOwnerProvider.class);
		for(MObjectOwnerProvider pro : pros.values()){
			if(pro!=null){
				providers.put(pro.getMType(), pro);
			}
		}
	}
	 
	 public static void regisityProvider(MObjectOwnerProvider provider){
		 providers.put(provider.getMType(), provider);
	 }
}
